package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;







public class Company {
	String cid;
	String cname;
	String cpw;
	String cemail;
	public String getCid() {
		return cid;
	}
	public Company(String cid, String cname, String cpw, String cemail) {
		super();
		this.cid = cid;
		this.cname = cname;
		this.cpw = cpw;
		this.cemail = cemail;
	}
	public Company(){
		
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public String getCpw() {
		return cpw;
	}
	public void setCpw(String cpw) {
		this.cpw = cpw;
	}
	public String getCemail() {
		return cemail;
	}
	public void setCemail(String cemail) {
		this.cemail = cemail;
	}
	public boolean login1(String cid,String cpw) throws SQLException{
    	String sql = "select * from Company where cid = "+cid+" and cpw = '"+cpw+"'";
		 ResultSet rs = DataConection.getStat().executeQuery(sql);
		 if(rs.next())
			 return true;
		 return false;
	}
	public boolean login2(String cemail,String cpw) throws SQLException{
    	String sql = "select * from Company where cemail ='"+cemail+"'and cpw = "+cpw+"";
		 ResultSet rs = DataConection.getStat().executeQuery(sql);
		 if(rs.next())
			 return true;
		 return false;
	}
	public void addCom(Company c) throws SQLException{
		String sql = "insert into company(cid,cname,cpw,cemail)values('"+c.getCid()+"','"+
		c.getCname()+"',"+c.getCpw()+",'"+c.getCemail()+"')";
		DataConection.getStat().executeUpdate(sql);
	
	}
	public ArrayList<User> searchUser(String msg) throws SQLException {
		ArrayList<User> users = new ArrayList<User>();
		String sql = "";
		try{
			int n = Integer.parseInt(msg);
			sql = "select * from user where uid = " + n;
		}catch(Exception e){
			sql = "select * from user where uname like '%"+ msg +"%'";
		}
		ResultSet rs = DataConection.getStat().executeQuery(sql);
		while(rs.next()){
			users.add(new User(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)));
		}
		return users;
		
	}
	public ArrayList<String> getCom(String cid) throws SQLException{
		String sql = "";
		sql = "select * from company ";
		ArrayList<String>  details = new ArrayList<String>();
		ResultSet rs = DataConection.getStat().executeQuery(sql);
		while(rs.next()){
			details.add(rs.getString(1));
			details.add(rs.getString(2));
			details.add(rs.getString(3));
			details.add(rs.getString(4));
		}
		return details;
	}
	public ArrayList<String> getInformation(String iid) throws SQLException{
		String sql = "";
		sql = "select * from information ";
		ArrayList<String>  details = new ArrayList<String>();
		ResultSet rs = DataConection.getStat().executeQuery(sql);
		while(rs.next()){
			details.add(rs.getString(1));
			details.add(rs.getString(2));
			details.add(rs.getString(3));
			details.add(rs.getString(4));
			details.add(rs.getString(5));
			details.add(rs.getString(6));
			details.add(rs.getString(7));
		}
		return details;
	}
	    public Information viewInform(String iid) throws SQLException{
	    	Information informations = null;
	    	String sql = "select * from information where iid = "+iid;
	    	ResultSet rs = DataConection.getStat().executeQuery(sql);
	    	if(rs.next()){
	    		informations = new Information(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7));
	    	}
	    	return informations;
	}
	    public void updateInform(String iid,String izhiwei,String irequire,String imoney,String imainwork) throws SQLException{
	    	String sql;
			if(izhiwei.trim().equals("")){
				sql = "update information set izhiwei = " + izhiwei+"where iid = " + iid;
				DataConection.getStat().executeUpdate(sql);
			}else if(irequire.trim().equals("")){
				sql = "update information set irequire = " + irequire+"where iid = " + iid;
				DataConection.getStat().executeUpdate(sql);
			}
			else if(imoney.trim().equals("")){
				sql = "update information set imoney = " + imoney+"where iid = " + iid;
				DataConection.getStat().executeUpdate(sql);
			}
			else if(imainwork.trim().equals("")){
				sql = "update information set imainwork = "  +imainwork+"where iid = " + iid;
				DataConection.getStat().executeUpdate(sql);
			}else{
				sql = "update information set izhiwei = " + izhiwei+ " where iid = " + iid;
				DataConection.getStat().executeUpdate(sql);
				sql = "update information set irequire = " + irequire+"where iid = "+iid;
				DataConection.getStat().executeUpdate(sql);
				System.out.println(sql);
				sql = "update information set imoney = " + imoney+"where iid = " + iid;
				DataConection.getStat().executeUpdate(sql);
				System.out.println(sql);
				sql = "update information set imainwork = " + imainwork+"where iid = " + iid;
				System.out.println(sql);
				DataConection.getStat().executeUpdate(sql);
//	    	String sql = "update information set iid='"+iid;
//			DataConection.getStat().executeUpdate(sql);
	    }
	    }
	    public ArrayList<Applicant> searchApplicant(String msg) throws SQLException {
			ArrayList<Applicant> applicants = new ArrayList<Applicant>();
			String sql = "select * from applicant";
			ResultSet rs = DataConection.getStat().executeQuery(sql);
			while(rs.next()){
				applicants.add(new Applicant(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4)));
			}
			return applicants;
			
		}

}
